#include "util/util.h"
#include <assert.h>
using namespace util;
const int SCR_W = 640;
const int SCR_H = 480;
CObj obj;
float ang = 0;
float scale = 1.0f;
bool expand = false;
CTexture tex;
CCamera cam;

void init()
{
	glViewport(0, 0, 640, 480);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClearDepth(1.0);
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_NORMALIZE);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	
	assert(obj.load("res/girl.obj"));
	//assert(obj.load("res/room.obj"));
	//obj.build_normal();
	tex.set_filter_low();   // glGenerateMipmap doesn't work on ATI as of 2011
	tex.load_bmp("res/rock.bmp");
}
void frame()
{

}
void render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	cam.perspective(45.0f, (float)SCR_W/SCR_H, 0.1f, 1000.f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	// setup material
	static const float MAT_COLOR[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	static const float MAT_SPECULAR[] = { 0.3f, 0.3f, 0.3f, 1.0f };
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, MAT_COLOR);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, MAT_SPECULAR);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128.0f);
	
	// setup lights
	static const float WHITE[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//	static const float RED[] = { 1.0f, 0.0f, 0.0f, 1.0f };
//	static const float GREEN[] = { 0.0f, 1.0f, 0.0f, 1.0f };
//	static const float BLUE[] = { 0.0f, 0.0f, 1.0f, 1.0f };
//	static const float YELLOW[] = { 1.0f, 1.0f, 0.0f, 1.0f };
	
	glEnable(GL_LIGHTING);
	
	glEnable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, WHITE);
	
	glVertexPointer(3, GL_FLOAT, 0, obj.get_vertex());
	glNormalPointer(GL_FLOAT, 0, obj.get_normal());
	
	glTranslatef(0.0f, -2.0f, -10.0f);
	glRotatef(ang, 0, 1, 0);
	ang += 1.5f;
	
	/*
		void glDrawArrays(GLenum mode, GLint first, GLsizei count);
		use count number VERTEX to render MODE type primitive
	*/
	glDrawArrays(GL_TRIANGLES, 0, obj.vertex_num());
}

int main()
{
	g_p_system = new CSystem(SCR_W, SCR_H, "Model Render by qq.d.y");
	g_p_system->set_callback(CSystem::INIT, init);
	g_p_system->set_callback(CSystem::RENDER, render);
	g_p_system->set_callback(CSystem::FRAME, frame);
	g_p_system->run();
	return 0;
}